package degree;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;

import student.Student;
import department.Department;
import descriptor.DegreeDescriptor;
import descriptor.FeedbackListDescriptor;
import feedback.Feedback;

/**
 * DegreeBean
 * 
 * 
 * The DegreeBean class implements all the methods that a remote client expect to find
 * when it calls the interface (Degree).
 * 
 * Note :  -We use @SuppressWarnings("unchecked") to avoid the useless warnings
 */
@SuppressWarnings("unchecked")
public abstract class DegreeBean implements EntityBean{
	
	// <ATTRIBUTES>
	
	// simple attributes
	public abstract int getId();
	public abstract void setId(int id);
	
	public abstract String getName();
	public abstract void setName(String name);
	
	public abstract String getInfos();
	public abstract void setInfos(String infos);
	
	public abstract Collection getStudents();
	public abstract void setStudents(Collection students);
	
	public abstract Collection getFeedbacks();
	public abstract void setFeedbacks(Collection feedbacks);
	
	public abstract Department getDepartment();
	public abstract void setDepartment(Department department);
	// </ATTRIBUTES>
	
	
	public DegreeDescriptor getDescriptor(){
		int[] feedbackIds = new int[getFeedbacks().size()];
		int i=0;
		for (Object o: getFeedbacks()){
			feedbackIds[i] = ((Feedback)o).getId();
			i++;
		}
		int[] studentIds = new int[getStudents().size()];
		i = 0;
		for (Object o: getStudents()){
			studentIds[i] = ((Student)o).getId();
			i++;
		}
		
		
		return new DegreeDescriptor(getId(),getName(),getInfos(),feedbackIds,getDepartment().getId(),studentIds);
	}
	
	public FeedbackListDescriptor getFeebackDescriptors(){
		Collection<Feedback> coll = getFeedbacks();
		FeedbackListDescriptor desc = new FeedbackListDescriptor();
		for(Feedback feed: coll){
			desc.addFeedbackDescriptor(feed.getDescriptor());
		}
		return desc;
	}
	// <BEANS METHODS>
	
	public Integer ejbCreate(String name, String infos) throws CreateException {
		setName(name);
		setInfos(infos);
		return 0;
	}

	
	public void ejbPostCreate(String name, String infos) throws CreateException {
		setFeedbacks(new Vector());
		setStudents(new Vector());
	}
	
	public void ejbPostCreate(String firstName, String name,  String passwd,  String email){
	}
	public void ejbActivate() throws EJBException, RemoteException {}

	public void ejbPassivate() throws EJBException, RemoteException {}

	public void ejbRemove() throws EJBException, RemoteException, RemoveException  {}

	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {}

	public void ejbLoad() throws EJBException, RemoteException {}

	public void ejbStore() throws EJBException, RemoteException {}

	public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {}

	public void unsetEntityContext() throws EJBException, RemoteException {}

	
	// </ BEANS METHODS>


}
